﻿using System.Collections.Generic;
using System.Threading.Tasks;
using DialogueManagerRuntime;
using Godot;
using kemocard.Scripts.Common;
using kemocard.Scripts.Module.Loading;
using kemocard.Scripts.Module.Run;
using kemocard.Scripts.MVC;

namespace kemocard.Scripts.Story;

public class TutorialStory : StoryScript
{
    public override void OnBeginStory()
    {
        var ctr = GameCore.ControllerMgr.GetModule<RunController>(ControllerType.Run);
        if (ctr == null) return;
        ctr.AddCharacter("Star");
        ctr.AddCharacter("Seth");
    }

    public override void OnEnterGameScene(int stage)
    {
        var mod = GameCore.ControllerMgr.GetControllerModel<RunModel>(ControllerType.Run);
        if (mod == null) return;
        if (stage != 1 || (bool)mod.StoryData.GetValueOrDefault("READ_TUTORIAL1", false)) return;
        using var res = ResourceLoader.Load(StaticUtil.GetDialoguePath("tutorial1"));
        if (res == null) return;
        DialogueManager.DialogueEnded += SetStoryData1;
        DialogueManager.ShowDialogueBalloon(res);
    }

    private static void SetStoryData1(Resource dialogueResource)
    {
        var mod = GameCore.ControllerMgr.GetControllerModel<RunModel>(ControllerType.Run);
        if (mod == null) return;
        mod.StoryData["READ_TUTORIAL1"] = true;
        DialogueManager.DialogueEnded -= SetStoryData1;
    }

    public override async Task OnStoryOnBattleStart()
    {
        var res = ResourceLoader.Load(StaticUtil.GetDialoguePath("tutorial2"));
        if (res == null) return;
        DialogueManager.ShowDialogueBalloon(res);
        await GameCore.Root.ToSignal(DialogueManager.Instance, "dialogue_ended");
    }

    public override async Task OnStoryOnBattleWin()
    {
        GameCore.EventBus.ClearByEventName(CommonEvent.BattleEvent_EndBattle);
        var res = ResourceLoader.Load(StaticUtil.GetDialoguePath("tutorial4"));
        if (res == null) return;
        DialogueManager.ShowDialogueBalloon(res);
        await GameCore.Root.ToSignal(DialogueManager.Instance, "dialogue_ended");
        // GameCore.ViewMgr.CloseAllView();
        GameCore.EventBus.AddEvent(this, CommonEvent.BattleEvent_EndBattle, CheckEndStory);
    }

    public override List<ActionStruct> GenerateActions()
    {
        return
        [
            new ActionStruct
            {
                Id = "TutorialBattle",
                Description = "教学战斗"
            }
        ];
    }

    public override Task OnAction(string action)
    {
        if (action == "TutorialBattle")
        {
            GameCore.ControllerMgr.SendUpdate(ControllerType.Battle, CommonEvent.StartBattle_BY_PRESET,
                "tutorial_battle");
        }

        return Task.CompletedTask;
    }

    public override void OnLoad()
    {
        GameCore.EventBus.AddTempEvent(CommonEvent.BattleEvent_ConfirmBtnPressed, OnConfirmBtnPressed);
    }

    private static void OnConfirmBtnPressed(object obj)
    {
        var res = ResourceLoader.Load(StaticUtil.GetDialoguePath("tutorial3"));
        if (res == null) return;
        DialogueManager.ShowDialogueBalloon(res);
    }

    private void CheckEndStory(object obj)
    {
        GameCore.EventBus.RemoveEvent(this, CommonEvent.BattleEvent_EndBattle, CheckEndStory);
        // GameCore.ViewMgr.CloseView(ViewType.GameScene);
        GameCore.ViewMgr.CloseAllView();
        var runMod = GameCore.ControllerMgr.GetControllerModel<RunModel>(ControllerType.Run);
        runMod?.GiveUp();
        SaveMgr.SetGlobalBool("HAS_TUTORIAL", true);
        var mod = GameCore.ControllerMgr.GetControllerModel<LoadingModel>(ControllerType.Loading);
        if (mod == null) return;
        mod.ViewType = ViewType.MenuScene;
        mod.SceneName = "MenuScene";
        GameCore.ControllerMgr.SendUpdate(ControllerType.Loading, CommonEvent.ShowLoadingScene, mod);
    }
}

public class RogueStory : StoryScript
{
    public override void OnBeginStory()
    {
        var ctr = GameCore.ControllerMgr.GetModule<RunController>(ControllerType.Run);
        if (ctr == null) return;
        ctr.AddUnhandledReward(["4", "5", "6", "7"]);
    }

    public override List<ActionStruct> GenerateActions()
    {
        return [];
    }

    public override Task OnAction(string action)
    {
        return Task.CompletedTask;
    }
}